package com.fl.springcloud.controller;

import com.fl.springcloud.entities.CommonResult;
import com.fl.springcloud.entities.PayMent;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
import java.net.URI;
import java.util.List;

@RestController
public class OrderController {
    @Resource
    private RestTemplate template;
//    public static final String PAYMENT_URL="http://localhost:8001";
    public static final String PAYMENT_URL = "http://CLOUD-PAYMENT-SERVICE";
//    @Resource
//    LoadBalanced balancedl;

    @GetMapping(value = "/consumer/payment/save")
    public CommonResult<PayMent> save(PayMent payMent){
        System.out.println(payMent);
        return template.postForObject(PAYMENT_URL + "/payment/save",payMent, CommonResult.class);
    }

    @GetMapping(value = "/consumer/getPayment/{id}")
    public CommonResult<PayMent> getPaymentById(@PathVariable("id") Long id){
        return template.getForObject(PAYMENT_URL + "/getPayment/"+id, CommonResult.class);
    }

    @GetMapping(value = "/consumer/getEntity/{id}")
    public CommonResult<PayMent> getPaymentById2(@PathVariable("id") Long id){
        ResponseEntity<CommonResult> entity = template.getForEntity(PAYMENT_URL + "/getPayment/" + id, CommonResult.class);
        if (entity.getStatusCode().is2xxSuccessful()){
            return entity.getBody();
        }else {
            return new  CommonResult<>(444,"响应失败！");
        }
    }
}
